//
//  SLLoginVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/10.
//

#import "SLLoginVC.h"
#import "SLRegisterVC.h"
#import "SLHomePageVC.h"
@interface SLLoginVC ()

@end

@implementation SLLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.tf becomeFirstResponder];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    self.title = kLocalString(@"Sign in");
    _sureBtn.btnStyle = 1;
    [_fotgetBtn setLineTextWith:kLocalString(@"Forgot password?")];
    [_sureBtn setTitle:kLocalString(@"Continue") forState:UIControlStateNormal];
    self.bgView.layer.cornerRadius = 20;
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.borderColor = kHexColor(@"#707070").CGColor;
    self.bgView.layer.borderWidth = 1;
    if (_step == 1) {
        [_tf setPlaceholderWithPlaceholderText:kLocalString(@"your@email.com") withColor:KColor_placeholdar withFont:kFont(16)];
        _tipLbl.text = kLocalString(@"Please enter the Email address that you used to create the account.");
        _fotgetBtn.hidden = YES;
        [_tf showCloseBtn];
    }else if (_step == 2){
        [_tf setPlaceholderWithPlaceholderText:kLocalString(@"Password") withColor:KColor_placeholdar withFont:kFont(16)];
        _tipLbl.text = kLocalString(@"Please enter the information that you used to creat the account.");
        _fotgetBtn.hidden = NO;
        _tf.secureTextEntry = YES;
        [_tf showEyeBtn];
    }
}

- (IBAction)sureBtnAction:(id)sender {
    if (_step == 1) {
        _userModel = [[UserInfo alloc] init];
        _userModel.email = _tf.text;
        if (_userModel.email.length == 0) {
            kShowToast(@"Enter your email");
            return;
        }
        SLLoginVC *vc = [[SLLoginVC alloc] init];
        vc.step = 2;
        vc.userModel = _userModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (_step == 2){//登录
        NSDictionary *params = @{@"email":_userModel.email,
                                 @"password":_tf.text};
        if (![_tf.text checkPassword]) {
            return;
        }
        [ZCTool showLoadingOnKeyWindow];
        [ZKHttpManager postWithPath:@"/msl_user/login" params:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
            [ZCTool hideLoadingOnKeyWindow];
            if (!isSuccessed) {
                kShowToast(message);
                return;
            }
            NSString *token = kSTRING(result[@"result"][@"token"]);
            [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"userToken"];
            KPostNotification(KNotificationLoginStateChange, @YES);
        }];
    }
}

- (IBAction)forgetAction:(id)sender {
    SLRegisterVC *vc = [[SLRegisterVC alloc] init];
    vc.step = 99;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
